/*
*    UNIVERSIDADE DE SÃO PAULO.
*    Author: Marco Aurélio Gerosa (gerosa@ime.usp.br)
*    This project was/is sponsored by RNP and FAPESP.
*
*    This file is part of Groupware Workbench (http://www.groupwareworkbench.org.br).
*
*    Groupware Workbench is free software: you can redistribute it and/or modify
*    it under the terms of the GNU Lesser General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    Groupware Workbench is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public License
*    along with Swift.  If not, see <http://www.gnu.org/licenses/>.
*/
package br.org.groupwareworkbench.eureka.hardware;

import javax.servlet.http.HttpSession;

import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Post;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.view.Results;
import br.org.groupwareworkbench.collablet.coord.user.User;
import br.org.groupwareworkbench.core.framework.WidgetInfo;
import br.org.groupwareworkbench.core.routing.Auth;

@Resource
public class HardwareMgrController {
    
    private final Result result;
    private final WidgetInfo info;
    private final HttpSession session;

    public HardwareMgrController(Result result, WidgetInfo info, HttpSession session) {
        this.result = result;
        this.info = info;
        this.session = session;
    }
    
    @Get
    @Path(value = "/groupware-workbench/{hardwareMgr}/hardware/{id}")
    public void retrieve(HardwareMgrInstance hardwareMgr, String id) {
        Hardware hardware = hardwareMgr.getHardwareById(id);
        if (hardware == null) {
            result.include("errorMessage", "Equipamento não encontrado!");
            result.redirectTo("/");
            return;
        }

        addIncludes(hardwareMgr);
        result.include("hardware", hardware);
        result.use(Results.representation()).from(hardware).serialize();
    }
    
    @Get
    @Auth(type = Auth.Permission.EVERYBODY)
    @Path(value = "/groupware-workbench/hardwareTeste")
    public void teste(){
        
    }
    
    @Post
    @Path(value = "/groupware-workbench/{hardwareMgr}/hardware/{id}/maintenance")
    public void addMaintenance(final HardwareMgrInstance hardwareMgr, final String id) {
        Hardware hardware = hardwareMgr.getHardwareById(id);
        if (hardware == null) {
            result.notFound();
            return;
        }
        
        final User user = (User) session.getAttribute("userLogin");
        
        hardwareMgr.addMaintenance(hardware, user);
        hardwareMgr.save(hardware);
        hardwareMgr.getCollablet().processWidgets(info, hardware);

        addIncludes(hardwareMgr);
        result.use(Results.logic()).redirectTo(HardwareMgrController.class).retrieve(hardwareMgr, id);
    }
    
    @Post
    @Path(value = "/groupware-workbench/{hardwareMgr}/hardware")
    public void save(final HardwareMgrInstance hardwareMgr, final Hardware hardware) {
        hardwareMgr.save(hardware);
        hardwareMgr.getCollablet().processWidgets(info, hardware);

        addIncludes(hardwareMgr);
        result.use(Results.logic()).redirectTo(HardwareMgrController.class).retrieve(hardwareMgr, hardware.getGatiCode());
    }
    
    private void addIncludes(HardwareMgrInstance hardwareMgr) {
        result.include("hardwareMgr", hardwareMgr);
        hardwareMgr.getCollablet().includeDependencies(result);
    }
    
}
